package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.BusinessHttpImpl;
import com.alan.libapi.business.dao.CallbackListener;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.view.iview.IVSHAWifiSubmit;

/**
 * @className: SHAWifiSubmitPS
 * @describtion: SHAWifiSubmitPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/27 18:31
 */
public class SHAWifiSubmitPS {

    private IVSHAWifiSubmit ivshaWifiSubmit;

    public SHAWifiSubmitPS(IVSHAWifiSubmit ivshaWifiSubmit) {
        this.ivshaWifiSubmit = ivshaWifiSubmit;
    }

    /**
     * 增加分享wifi
     */
    public void postAddWifi(){
        String wifiSSID = ivshaWifiSubmit.getWifiSSID();
        String wifiPassword = ivshaWifiSubmit.getWifiPassword();
        if(wifiPassword == null || TextUtils.isEmpty(wifiPassword)){
            ivshaWifiSubmit.toastMsg("密码不能为空");
            return;
        }else if(wifiPassword.length() < 8){
            ivshaWifiSubmit.toastMsg("密码长度不能小于8");
            return;
        }
        String wifiMac = ivshaWifiSubmit.getWifiMac();//2F4C3D6A2C3A
        double wifiLongitude = ivshaWifiSubmit.getWifiLongitude();
        double wifiLatitude = ivshaWifiSubmit.getWifiLatitude();
        int wifiAreaId = ivshaWifiSubmit.getAreaId();
        String wifiAddress = ivshaWifiSubmit.getWifiAdressStr();
        ivshaWifiSubmit.showProgress();
        BusinessHttpImpl.getInstance().postAddWifi(GlobalField.userSession, wifiSSID, wifiPassword, wifiMac, wifiLongitude, wifiLatitude,
                wifiAreaId, wifiAddress, new CallbackListener<Void>() {
                    @Override
                    public void onSuccess(Void result) {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ivshaWifiSubmit.hideProgress();
                                ivshaWifiSubmit.submitSuccess();
                            }
                        });
                    }

                    @Override
                    public void onFailure(int errorCode, final String msg) {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ivshaWifiSubmit.hideProgress();
                                ivshaWifiSubmit.toastMsg(msg);
                            }
                        });
                    }
                });
    }

    /**
     * 更新wifi
     */
    public void postUpdateWifi() {
        int wifiId = ivshaWifiSubmit.getWifiId();
        String wifiSSID = ivshaWifiSubmit.getWifiSSID();
        String wifiPassword = ivshaWifiSubmit.getWifiPassword();
        if(wifiPassword == null || TextUtils.isEmpty(wifiPassword)){
            ivshaWifiSubmit.toastMsg("密码不能为空");
            return;
        }else if(wifiPassword.length() < 8){
            ivshaWifiSubmit.toastMsg("密码长度不能小于8");
            return;
        }
        String wifiMac = ivshaWifiSubmit.getWifiMac();//2F4C3D6A2C3A
        double wifiLongitude = ivshaWifiSubmit.getWifiLongitude();
        double wifiLatitude = ivshaWifiSubmit.getWifiLatitude();
        int wifiAreaId = ivshaWifiSubmit.getAreaId();
        String wifiAddress = ivshaWifiSubmit.getWifiAdressStr();
        ivshaWifiSubmit.showProgress();
        BusinessHttpImpl.getInstance().postUpdateWifi(GlobalField.userSession, wifiId, wifiSSID, wifiPassword, wifiLongitude, wifiLatitude, wifiAreaId, wifiAddress,
                new CallbackListener<Void>() {
                    @Override
                    public void onSuccess(Void result) {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ivshaWifiSubmit.hideProgress();
                                ivshaWifiSubmit.submitSuccess();
                            }
                        });
                    }

                    @Override
                    public void onFailure(int errorCode, String msg) {
                        GlobalField.handler.post(new Runnable() {
                            @Override
                            public void run() {
                                ivshaWifiSubmit.hideProgress();
                            }
                        });
                    }
                });
    }
}
